package com.xyy.saas.payment.cores.bo;

import com.xyy.saas.payment.cores.enums.PayModeEnum;
import com.xyy.saas.payment.cores.enums.TransactionStatusEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class TransactionResultBO implements Serializable {
    private static final long serialVersionUID = -4925230001714859855L;

    /**
     * 支付交易
     */
    private String tradeNo;
    /**
     * 状态
     */
    private String status;
    /**
     * 金额
     */
    private Integer amount;
    /**
     * 渠道
     */
    private String channel;
    /**
     * 成功时间 unix 时间
     */
    private Long successTime;
    /**
     * 银行交易单号
     */
    private String bankChargeNo;
    /**
     * 渠道交易单号
     */
    private String channelChargeNo;
    /**
     * 业务单号
     */
    private String businessOrderNo;
    /**
     * 收款商户号
     */
    private String merchantId;
    /**
     * 支付方式
     */
    private String payMode;

    /**
     * 平安支付查询结果
     */
    public static TransactionResultBO buildPayByPingan(Map<String, Object> param, String prePayOrderExtra) {
        TransactionResultBO resultBO = new TransactionResultBO();
        String traderOrderNo = (String) param.get("TraderOrderNo");
        resultBO.setTradeNo(traderOrderNo.startsWith("T") ? traderOrderNo.substring(1) : traderOrderNo);
        resultBO.setStatus(TransactionStatusEnum.convertPingPayStatus((String) param.get("OrderStatus")).getCode());
        resultBO.setAmount(Integer.parseInt((String) param.get("TranAmt")));
        Object payModeNo = param.get("PayModeNo");
        String payMode = null;

        if(payModeNo != null) {
            payMode = (String) payModeNo;

            if(payMode.contains("Weixin")) {
                resultBO.setChannel("pingan_wx");
            }
            else if(payMode.contains("Alipay")) {
                resultBO.setChannel("pingan_alipay");
            }
            else if(payMode.contains("CreditPay")) {
                resultBO.setChannel("pingan_credit");
            }
            else {
                new RuntimeException("pingan unknown PayModeNo");
            }
        }
        else {
            new RuntimeException("pingan unknown PayModeNo");
        }

        // 外部填充
        resultBO.setSuccessTime((Long) param.get("PaySuccessTime"));
        resultBO.setBankChargeNo((String) param.get("BankOrderNo"));
        resultBO.setChannelChargeNo((String) param.get("ChannelOrderNo"));
        resultBO.setMerchantId((String) param.get("StoreId"));
        resultBO.setPayMode(PayModeEnum.convertPingan(payMode, resultBO.getChannel(), prePayOrderExtra));
        return resultBO;
    }

    @Deprecated
    public static TransactionResultBO build(String tradeNo, String status) {
        return build(tradeNo, status, null, null, null, null, null, null);
    }

    @Deprecated
    public static TransactionResultBO build(String tradeNo, String status, Integer amount, String channel, Long successTime, String channelChargeNo, String businessOrderNo, String merchantId) {
        TransactionResultBO item = new TransactionResultBO();
        item.setTradeNo(tradeNo);
        item.setStatus(status);
        item.setAmount(amount);
        item.setChannel(channel);
        item.setSuccessTime(successTime);
        item.setChannelChargeNo(channelChargeNo);
        item.setBusinessOrderNo(businessOrderNo);
        item.setMerchantId(merchantId);
        return item;
    }
}
